package top.yueyazhui.vhr.service;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yueyazhui.vhr.bean.ResponsePage;
import top.yueyazhui.vhr.mapper.EmployeeMapper;
import top.yueyazhui.vhr.model.Employee;
import top.yueyazhui.vhr.model.MailSendConstant;
import top.yueyazhui.vhr.model.MailSendLog;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MailSendLogService mailSendLogService;

    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    public ResponsePage getAllEmployeeByPage(Integer page, Integer size, Employee employee, Date[] beginDateScope) {
        if(page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getAllEmployeeByPage(page, size, employee, beginDateScope);
        Long total = employeeMapper.getTotal(employee, beginDateScope);
        ResponsePage response = new ResponsePage();
        response.setData(data);
        response.setTotal(total);
        return response;
    }

    public Integer insert(Employee employee) {
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        double month = (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract))) * 12 + (Double.parseDouble(monthFormat.format(endContract)) - Double.parseDouble(monthFormat.format(beginContract)));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(month / 12)));
        int result = employeeMapper.insertSelective(employee);
        if(result == 1) {
            Employee emp = employeeMapper.getEmployeeById(employee.getId());
            String msgId = UUID.randomUUID().toString();
            MailSendLog mailSendLog = new MailSendLog();
            mailSendLog.setMsgId(msgId);
            mailSendLog.setCreateTime(new Date());
            mailSendLog.setExchange(MailSendConstant.MAIL_EXCHANGE_NAME);
            mailSendLog.setRouteKey(MailSendConstant.MAIL_ROUTING_KEY_NAME);
            mailSendLog.setEmpId(emp.getId());
            mailSendLog.setTryTime(new Date(System.currentTimeMillis() + 1000 * 60 * MailSendConstant.MSG_TIMEOUT));
            mailSendLog.setCount(0);
            mailSendLogService.insert(mailSendLog);
            rabbitTemplate.convertAndSend(MailSendConstant.MAIL_EXCHANGE_NAME, MailSendConstant.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(msgId));
        }
        return result;
    }

    public Integer getMaxWorkId() {
        return employeeMapper.getMaxWorkId();
    }

    public Integer delete(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    public Integer update(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public Integer inserts(List<Employee> list) {
        return employeeMapper.inserts(list);
    }

    public List<Employee> getAllEmployee() {
        return employeeMapper.getAllEmployee();
    }

    public ResponsePage getAllEmployeeByPageWithSalary(Integer page, Integer size) {
        if(page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getAllEmployeeByPageWithSalary(page, size);
        Long total = employeeMapper.getTotalWithSalary();
        ResponsePage response = new ResponsePage();
        response.setData(data);
        response.setTotal(total);
        return response;
    }

    public Integer updateEmployeeSalaryById(Integer employeeId, Integer salaryId) {
        return employeeMapper.updateEmployeeSalaryById(employeeId, salaryId);
    }

    public Employee getEmployeeById(Integer empId) {
        return employeeMapper.getEmployeeById(empId);
    }
}
